package com.qqtech.qquan.user.controller;

import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.qqtech.admin.context.AdminPage;
import com.qqtech.core.frame.model.PageKit;
import com.qqtech.core.frame.model.PageOrder;
import com.qqtech.core.frame.service.BaseService;
import com.qqtech.qquan.sys.controller.BaseAdminController;
import com.qqtech.qquan.user.model.UserBase;
import com.qqtech.qquan.user.model.UserSuggest;
import com.qqtech.qquan.user.model.UserSuggestQuery;
import com.qqtech.qquan.user.model.UserSuggestReplyQuery;
import com.qqtech.qquan.user.model.UserSuggestReplyVo;
import com.qqtech.qquan.user.model.UserSuggestVo;
import com.qqtech.qquan.user.service.UserBaseService;
import com.qqtech.qquan.user.service.UserSuggestReplyService;
import com.qqtech.qquan.user.service.UserSuggestService;

@Controller
@RequestMapping(value = "/user/suggest")
public class UserSuggestController extends BaseAdminController {
	@Resource
	private UserSuggestService userSuggestService;
	
	@Resource
	private UserBaseService userBaseService;

	@Resource
	private UserSuggestReplyService userSuggestReplyService;

	@Override
	protected BaseService<UserSuggest> getBaseService() {
		return userSuggestService;
	}

	/**
	 * 列表页面
	 * 
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "/list", method = RequestMethod.GET)
	public String list(Model model) {
		return "/user/suggest/usersuggest-list";
	}

	/**
	 * 获取列表数据
	 * 
	 * @param query
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(value = "/list", method = RequestMethod.POST)
	@ResponseBody
	public AdminPage list(UserSuggestQuery query, HttpServletRequest request, HttpServletResponse response) {
		AdminPage adminPage = new AdminPage();
		String rows = (String) request.getParameter("rows"); // 每页条数
		query.setSize(Integer.valueOf(rows));
		Pageable pageKit = new PageKit(query.getPage(), query.getSize(), new PageOrder(Direction.DESC, "id"));
		Page<UserSuggestVo> page = userSuggestService.queryPageListAdmin(query, pageKit);
		UserBase userBase;
		for (UserSuggestVo userSuggestVo : page) {
			if(null!=userSuggestVo.getCreatorId()){
				userBase=userBaseService.getById(userSuggestVo.getCreatorId());
				userSuggestVo.setNick(userBase.getNick());
			}else{
				userSuggestVo.setNick("访客");
			}
		}
		adminPage.setRows(page.getContent());
		adminPage.setTotal(Integer.valueOf(page.getTotalElements() + ""));
		return adminPage;
	}

	@RequestMapping(value = "/detail", method = RequestMethod.GET)
	public String list(Integer id, Model model) {
		UserSuggestVo detail = userSuggestService.getById(id);
		UserBase userBase=userBaseService.getById(detail.getCreatorId());
		detail.setImgString(detail.getImg(),"");
		detail.setNick(userBase.getNick());
		UserSuggestReplyQuery userSuggestReplyQuery = new UserSuggestReplyQuery();
		userSuggestReplyQuery.setSuggestId(id);
		List<UserSuggestReplyVo> replys = userSuggestReplyService.queryList(userSuggestReplyQuery);
		for (UserSuggestReplyVo userSuggestReplyVo : replys) {
			userSuggestReplyVo.setImgString(userSuggestReplyVo.getImg(),"");
		}
		model.addAttribute("detail", detail);
		model.addAttribute("replys", replys);
		return "/user/suggest/usersuggest-detail";
	}
}
